<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建日期：">
        <el-date-picker @change="dateChange" v-model="time" value-format="yyyy-MM-dd" type="daterange" range-separator="至"
          start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div style="display: flex; justify-content: space-between;width: 100%;">
      <div class="left">
        <el-button :class="listFlag == 0 ? 'active' : 'unactive'" @click="checkList(0)">小区模型</el-button>
        <el-button :class="listFlag == 1 ? 'active' : 'unactive'" @click="checkList(1)">楼栋模型</el-button>
        <el-button :class="listFlag == 2 ? 'active' : 'unactive'" @click="checkList(2)">单元模型</el-button>
        <el-button :class="listFlag == 3 ? 'active' : 'unactive'" @click="checkList(3)">住户模型</el-button>
      </div>
      <div class="right">
        <el-button class="btn" @click="handleAdd1" icon="zidingyimoxing">添加模型</el-button>
        <el-button class="btn" icon="el-icon-error" @click="piliangDel">批量删除</el-button>
        <el-button type="primary" size="mini" icon="el-icon-search" @click="showSearch = !showSearch"></el-button>
        <el-button type="primary" size="mini" icon="el-icon-refresh-right" @click="getList"></el-button>
      </div>
    </div>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['custommodel:custommodel:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['custommodel:custommodel:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['custommodel:custommodel:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['custommodel:custommodel:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <!-- 一级模型列表 -->
    <el-table v-loading="loading" :data="custommodelList1" @cell-click="goSubordinate"
      @selection-change="handleSelectionChange">
      <el-table-column v-if="selectFlag" type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index"></el-table-column>
      <el-table-column label="模型名称" align="center" prop="modelName" />
      <!-- <el-table-column label="算法名称" align="center" prop="arithmeticName" /> -->
      <!-- <el-table-column label="算法位置" align="center" prop="arithmeticLocation" /> -->
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="该模型使用数" align="center" prop="useQuantity" />
      <!-- <el-table-column label="下级模型类型" v-if="listFlag !== 3" align="center" prop="childUseType" /> -->
      <el-table-column class-name="subordinate" label="下级模型数量" v-if="listFlag !== 3" align="center" prop="childCount" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status === "1" ? '启用' : '禁用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" v-hasPermi="['custommodel:custommodel:edit']">更新</el-button> -->
          <el-button size="mini" type="text" @click="edit1(scope.row)"
            v-hasPermi="['custommodel:custommodel:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['custommodel:custommodel:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total1 > 0" :total="total1" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <div>
      <el-button class="btn" @click="handleAdd2">添加模型</el-button>
      <el-button class="btn" icon="el-icon-error" @click="piliangDel1">批量删除</el-button>
    </div>

    <!-- 二级模型列表 -->
    <el-table v-loading="loading" :data="custommodelList2" @selection-change="handleSelectionChange1">
      <el-table-column v-if="selectFlag1" type="selection" width="55" align="center" show-overflow-tooltip />
      <el-table-column label="序号" align="center" type="index" show-overflow-tooltip></el-table-column>
      <el-table-column label="模型名称" align="center" prop="modelName" show-overflow-tooltip />
      <el-table-column label="创建人" align="center" prop="createBy" show-overflow-tooltip />
      <el-table-column show-overflow-tooltip label="创建时间" align="center" prop="createTime" />
      <el-table-column label="该模型使用数" align="center" prop="useQuantity" />
      <el-table-column v-if="listFlag == 3" label="住户面积(㎡)" align="center">
        <template slot-scope="scope">
          {{ scope.row.residentAreaStart }} - {{ scope.row.residentAreaEnd }}
        </template>
      </el-table-column>
      <el-table-column v-if="listFlag == 3" label="户型" align="center" prop="houseType" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.houseType === "1" ? '单层' : '复式' }}
        </template>
      </el-table-column>
      <el-table-column v-if="listFlag == 3" label="散热器类型" align="center" prop="radiatorType" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.radiatorType === "1" ? '地暖' : '片暖' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.status === "1" ? '启用' : '禁用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text">更新</el-button> -->
          <el-button size="mini" type="text" @click="edit2(scope.row)"
            v-hasPermi="['custommodel:custommodel:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['custommodel:custommodel:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total2 > 0" :total="total2" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />



    <!-- 添加或修改自定义模型对话框 -->
    <!-- 添加或修改一级模型 -->
    <el-dialog :title="title" :visible.sync="oneOpen" width="700px" append-to-body>
      <el-form ref="formOne" :model="formOne" :rules="rules1" label-position="left" label-width="80px"
        style="display: flex; flex-wrap: wrap;justify-content: space-between;">
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="formOne.modelName" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formOne.status" placeholder="请选择模型状态">
            <!-- <el-option v-for="dict in dict.type.model_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option> -->
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="模型级别" prop="modelLevel">
          <el-select v-model="formOne.modelLevel" placeholder="请选择模型级别">
            <el-option label="一级模型" value="1"></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="模型类型" prop="modelType">
          <el-select v-model="formOne.modelType" placeholder="请选择模型类型">
            <el-option label="小区模型" value="0"></el-option>
            <el-option label="楼栋模型" value="1"></el-option>
            <el-option label="单元模型" value="2"></el-option>
            <el-option label="住户模型" value="3"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="算法名称" prop="arithmeticName">
          <el-input v-model="formOne.arithmeticName" placeholder="请输入算法名称" />
        </el-form-item>
        <!-- <el-form-item label="算法位置" prop="arithmeticLocation">
          <el-input v-model="formOne.arithmeticLocation" placeholder="请输入算法位置" />
        </el-form-item> -->
        <el-form-item></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 添加或修改二级模型 -->
    <el-dialog :title="title" :visible.sync="twoOpen" width="700px" append-to-body>
      <el-form ref="formTwo" :model="formTwo" :rules="rules2" label-position="left" label-width="80px"
        style="display: flex; flex-wrap: wrap;justify-content: space-between;">
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="formTwo.modelName" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formTwo.status" placeholder="请选择模型状态">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="模型类型" prop="modelType">
          <el-select v-model="formTwo.modelType" placeholder="请选择模型类型">
            <el-option label="小区模型" value="0"></el-option>
            <el-option label="楼栋模型" value="1"></el-option>
            <el-option label="单元模型" value="2"></el-option>
            <el-option label="住户模型" value="3"></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="模型级别" prop="modelLevel">
          <el-select v-model="formTwo.modelLevel" placeholder="请选择模型级别">
            <el-option label="二级模型" value="2"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item v-if="listFlag == 3" label="户型" prop="houseType">
          <el-select v-model="formTwo.houseType" placeholder="请选择户型">
            <el-option v-for="dict in dict.type.house_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="listFlag == 3" label="散热器类型" prop="radiatorType">
          <el-select v-model="formTwo.radiatorType" placeholder="请选择散热器类型">
            <el-option v-for="dict in dict.type.radiator_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="listFlag == 3" label="面积开始范围" prop="residentAreaStart">
          <el-input v-model="formTwo.residentAreaStart" placeholder="开始范围" />
        </el-form-item>
        <el-form-item v-if="listFlag == 3" label="面积结束范围" prop="residentAreaEnd">
          <el-input v-model="formTwo.residentAreaEnd" placeholder="结束范围" />
        </el-form-item>
        <el-form-item label="算法名称" prop="arithmeticName">
          <el-input v-model="formTwo.arithmeticName" placeholder="请输入算法名称" />
        </el-form-item>
        <!-- <el-form-item label="算法位置" prop="arithmeticLocation">
          <el-input v-model="formTwo.arithmeticLocation" placeholder="请输入算法位置" />
        </el-form-item> -->
        <el-form-item></el-form-item>
        <!-- <el-form-item label="使用数量" prop="useQuantity" style="margin-right: auto;margin-left: 4.5%;">
          <el-input v-model="formTwo.useQuantity" placeholder="请输入使用数量" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustommodel, getCustommodel, delCustommodel, addCustommodel, updateCustommodel } from "@/api/customModel/customModel";
import { modleList } from "@/api/mock"
import data5 from '@/utils/data5.json'

export default {
  name: "Custommodel",
  dicts: ['radiator_type', 'model_type', 'house_type'],
  data() {
    return {
      listFlag: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      ids1: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      //一级模型
      total1: 0,
      //二级模型
      total2: 0,
      // 自定义模型表格数据
      // 一级模型
      custommodelList1: [],
      // 二级模型
      custommodelList2: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      oneOpen: false,
      twoOpen: false,
      //时间选择器时间
      time: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelName: null,
        modelType: "0",
        modelLevel: null,
        houseType: null,
        radiatorType: null,
        residentAreaStart: null,
        residentAreaEnd: null,
        status: null,
        arithmeticName: null,
        arithmeticLocation: null,
        useQuantity: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      //添加一级模型
      formOne: {
        modelLevel: '1',
      },
      //添加二级模型
      formTwo: {
        modelLevel: '2',
      },
      // 表单校验
      rules1: {
        modelName: [
          { required: true, message: "模型名称不能为空", trigger: "blur" }
        ],
        modelType: [
          { required: true, message: "模型类型不能为空", trigger: "change" }
        ],
        modelLevel: [
          { required: true, message: "模型级别不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        arithmeticName: [
          { required: true, message: "算法名称不能为空", trigger: "blur" }
        ],
        arithmeticLocation: [
          { required: true, message: "算法位置不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      rules2: {
        modelName: [
          { required: true, message: "模型名称不能为空", trigger: "blur" }
        ],
        modelType: [
          { required: true, message: "模型类型不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        arithmeticName: [
          { required: true, message: "算法名称不能为空", trigger: "blur" }
        ],
        arithmeticLocation: [
          { required: true, message: "算法位置不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      selectFlag: false,
      selectFlag1: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 切换列表
    checkList(num) {
      this.listFlag = num
      this.queryParams.modelType = num + ""
      this.getList()
    },
    /** 查询自定义模型列表 */
    getList() {
      this.loading = true;
      listCustommodel({ ...this.queryParams, modelLevel: "1" }).then(response => {
        this.custommodelList1 = response.rows;
        this.total1 = response.total;
        this.loading = false;
      }, err => {
        this.loading = false
      });
      listCustommodel({ ...this.queryParams, modelLevel: "2" }).then(response => {
        this.custommodelList2 = response.rows;
        this.total2 = response.total;
        this.loading = false;
      }, err => {
        this.loading = false
      });
    },
    /** 查询一级模型列表 */
    getFirstList() {
      this.loading = true;
      listCustommodel({ ...this.queryParams, modelLevel: "1" }).then(response => {
        this.custommodelList1 = response.rows;
        this.total1 = response.total;
        this.loading = false;
      }, err => {
        this.loading = false
      });
    },
    /** 查询二级模型列表 */
    getSecondaryList() {
      this.loading = true;
      listCustommodel({ ...this.queryParams, modelLevel: "2" }).then(response => {
        this.custommodelList2 = response.rows;
        this.total2 = response.total;
        this.loading = false;
      }, err => {
        this.loading = false
      });
    },
    // 取消按钮
    cancel() {
      this.oneOpen = false;
      this.twoOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formOne = {
        id: null,
        modelName: null,
        modelType: "0",
        modelLevel: '1',
        houseType: null,
        radiatorType: null,
        residentAreaStart: null,
        residentAreaEnd: null,
        status: null,
        arithmeticName: null,
        arithmeticLocation: null,
        useQuantity: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.formTwo = { modelLevel: '2' };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.startTime) {
        this.queryParams.startTime = this.time[0]
        this.queryParams.endTime = this.time[1] + " 23:59:59"
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        modelName: null,
        modelType: "0",
        modelLevel: null,
        houseType: null,
        radiatorType: null,
        residentAreaStart: null,
        residentAreaEnd: null,
        status: null,
        arithmeticName: null,
        arithmeticLocation: null,
        useQuantity: null,
        startTime: null,
        endTime: null,
      },
      this.listFlag = 0
      // this.reset()
      this.time = null
      // this.handleQuery();
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleSelectionChange1(selection) {
      this.ids1 = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd1() {
      this.reset();
      this.oneOpen = true;
      this.title = "添加一级自定义模型";
    },
    handleAdd2() {
      this.reset();
      this.twoOpen = true;
      this.title = "添加二级自定义模型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustommodel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改自定义模型";
      });
    },
    // 一级模型修改
    edit1(row) {
      // console.log(row);
      const id = row.id || this.ids
      getCustommodel(id).then(response => {
        this.formOne = response.data;
        this.formOne.modelLevel = "1"
        this.oneOpen = true;
        this.title = "修改自定义一级模型";
      });
    },
    // 二级模型修改
    edit2(row) {
      const id = row.id || this.ids
      getCustommodel(id).then(response => {
        this.formTwo = response.data;
        this.formTwo.modelLevel = "2"
        this.twoOpen = true;
        this.title = "修改自定义二级模型";
      });
    },
    /** 提交按钮 */
    // 一级模型
    submitForm1() {
      this.$refs["formOne"].validate(valid => {
        if (valid) {
          this.formOne.modelType = this.listFlag
          if (this.formOne.id != null) {
            updateCustommodel(this.formOne).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.oneOpen = false;
              this.getFirstList()
            });
          } else {
            addCustommodel(this.formOne).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.oneOpen = false;
              this.getFirstList()
            });
          }
        }
      });
    },
    // 二级模型
    submitForm2() {
      this.$refs["formTwo"].validate(valid => {
        if (valid) {
          this.formTwo.modelType = this.listFlag
          if (this.formTwo.id != null) {
            updateCustommodel(this.formTwo).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.twoOpen = false;
              this.getSecondaryList()
            });
          } else {
            addCustommodel(this.formTwo).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.twoOpen = false;
              this.getSecondaryList()
            });
          }
        }
      });
    },
    piliangDel1() {
      if (this.ids1.length === 0) {
        this.selectFlag1 = !this.selectFlag1
      } else {
        this.$modal.confirm('是否删除所选数据？').then(() => {
          return delCustommodel(this.ids1);
        }).then(() => {
          this.ids1 = []
          this.selectFlag1 = !this.selectFlag1
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      }
    },
    // 批量删除
    piliangDel() {
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag
      } else {
        this.$modal.confirm('是否删除所选数据？').then(() => {
          return delCustommodel(this.ids);
        }).then(() => {
          this.ids = []
          this.selectFlag = !this.selectFlag
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否删除当前数据？').then(function () {
        return delCustommodel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('custommodel/custommodel/exportCustomModel', {
        ...this.queryParams
      }, `custommodel_${new Date().getTime()}.xlsx`)
    },

    //前往下级模型
    goSubordinate(row, column) {
      if (column.label === '下级模型数量') {
        console.log(row)
        console.log(column)
      }
    },
    dateChange(e) {
      if (e) {
        this.queryParams.startTime = e[0]
        this.queryParams.endTime = e[1]
      } else {
        this.queryParams.startTime = null
        this.queryParams.endTime = null
      }
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .subordinate {
  cursor: pointer;
}
</style>
